#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 9888
#define MAX_BUFFER_SIZE 1024
static struct sockaddr_in server_addr;
static char session_header[20];
static int clientForInfo;
void send_OPTIONS(int client_sock, char response[])
{
    char request[MAX_BUFFER_SIZE] = {0};
    sprintf(request, "OPTIONS DianTP://%s:%d DianTP/0.5\r\nCSeq: 1\r\n\r\n", SERVER_IP, SERVER_PORT);
    send(client_sock, request, strlen(request), 0);
    printf("Sent OPTIONS request\n");
    if (recv(client_sock, response, MAX_BUFFER_SIZE, 0) > 0)
    {
        printf("Received response: %s\n", response);
    }
    memset(response, 0, MAX_BUFFER_SIZE);
}
int send_SETUP(int client_sock, char response[])
{
    int CLIENT_PORT = rand() % 8000 + 8000;
    char request[MAX_BUFFER_SIZE] = {0};
    sprintf(request, "SETUP DianTP://%s:%d DianTP/0.5\r\nCSeq: 2\r\nTransport: RTP/UDP;client_port=%d\r\n\r\n", SERVER_IP, SERVER_PORT, CLIENT_PORT);
    send(client_sock, request, strlen(request), 0);
    printf("Sent SETUP request\n");
    clientForInfo = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in client_addr;
    client_addr.sin_family = AF_INET;
    client_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    client_addr.sin_port = htons(CLIENT_PORT);
    int bind_result = bind(clientForInfo, (struct sockaddr *)&client_addr, sizeof(client_addr));

    if (bind_result < 0)
    {
        printf("Failed to bind port");
        exit(-1);
    }
    if (connect(clientForInfo, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
    {
        perror("Connect error");
        exit(-1);
    }
    if (recv(client_sock, response, MAX_BUFFER_SIZE, 0) > 0)
    {
        printf("Received response: %s\n", response);
    }
}
void send_PLAY(int client_sock, char response[]){
    char request[MAX_BUFFER_SIZE];
    // "DianTP/0.5 200 OK\r\nCSeq: 2\r\nTransport: RTP/UDP;unicast;client_port=4589\r\nSession: %d\r\n\r\n", session_id);
    char *version = strtok(response, " ");
    char *stateInteger = strtok(NULL, "\r\n");
    char *cseq_header = strtok(NULL, "\r\n");
    char *client = strtok(NULL, "\r\n");
    strcpy(session_header, strtok(NULL, "\r\n"));
    sprintf(request, "PLAY DianTP://%s:%d DianTP/0.5\r\nCSeq: 3\r\n%s\r\nRange: npt=0-\r\n\r\n", SERVER_IP, SERVER_PORT, session_header);
    send(client_sock, request, strlen(request), 0);
    printf("Sent PLAY request\n");
    memset(response, 0, MAX_BUFFER_SIZE);
    if (recv(client_sock, response, MAX_BUFFER_SIZE, 0) > 0)
    {
        printf("%s", response);
        memset(response, 0, MAX_BUFFER_SIZE);
    }
    sleep(1);
    if (recv(clientForInfo, response, MAX_BUFFER_SIZE, 0) > 0)
    {
        printf("%s", response);
    }
}
void send_TEARDOWN(int client_sock){
    printf("Please confirm that you want to send the TEARDOUN infomation.Y/N");
    char choice;
    scanf("%c", &choice);
    if (choice == 'Y')
    {
        char request[MAX_BUFFER_SIZE];
        sprintf(request, "TEARDOWN DianTP://%s:%d DianTP/0.5\r\nCSeq: 4\r\n%s\r\n\r\n", SERVER_IP, SERVER_PORT, session_header);
        send(client_sock, request, sizeof(request), 0);
        printf("Sent TEARDOWN request\n");
        sleep(1);
        char buffer[MAX_BUFFER_SIZE] = {0};
        if (recv(clientForInfo, buffer, MAX_BUFFER_SIZE, 0) >= 0)
        {
            printf("%s", buffer);
        }
    }
}
int main()
{
    srand(time(NULL));
    char response[MAX_BUFFER_SIZE];
    // 创建 socket
    int client_sock = socket(AF_INET, SOCK_STREAM, 0);

    // 设置服务器地址
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    server_addr.sin_port = htons(SERVER_PORT);

    // 连接服务器
    if (connect(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
    {
        perror("Connect error");
        exit(-1);
    }

    // 发送请求
    send_OPTIONS(client_sock, response);
    sleep(1);
    send_SETUP(client_sock, response);
    sleep(1);
    send_PLAY(client_sock, response);
    sleep(1);
    send_TEARDOWN(client_sock);
    close(client_sock);
    return 0;
}
